using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ST.Application.RoleServices;
using ST.Application.UserServices;
using ST.Application.UserServices.Dtos;

namespace Portal.Controllers
{
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;
        private readonly IRoleService _roleService;

        public UserController(IUserService userService,IRoleService roleService)
        {
            _userService = userService;
            _roleService = roleService;
        }
        // GET
        public IActionResult Index()
        {
            return View();
        }
        
        public async Task<IActionResult> GetUserByDepartment(Guid departmentId, int startPage, int pageSize)
        {
            var result =await _userService.GetUserByDepartment(departmentId, startPage, pageSize);
            var roles = _roleService.GetAllList();
            return Json(new
            {
                rowCount = result.TotalCount,
                pageCount = result.TotalPages,
                rows = result,
                roles = roles
            });
        }

        public async Task<IActionResult> Edit(UserDto dto, string roles)
        {
            try
            {
                if (dto.Id == Guid.Empty)
                {
                    dto.Id = Guid.NewGuid();
                }
                var userRoles = roles.Split(',').Select(role => new UserRoleDto() {UserId = dto.Id, RoleId = Guid.Parse(role)}).ToList();
                dto.UserRoles = userRoles;
                
                var result = false;
                if (await _userService.GetAsync(dto.Id) == null)
                {
                    result = await _userService.InsertAsync(dto);
                }
                else
                {
                    result = await _userService.UpdateAsync(dto);
                }

                return Json(result ? new { Result = "Success" } : new { Result = "Faild" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Faild", Message = ex.Message });

            }
        }

        public async Task<IActionResult> DeleteMuti(string ids)
        {
            try
            {
                var idArray = ids.Split(',');
                var delIds = idArray.Select(Guid.Parse).ToList();
                await _userService.DeleteBatchAsync(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public async Task<IActionResult> Delete(Guid id)
        {
            try
            {
                await _userService.DeleteAsync(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public async Task<IActionResult> Get(Guid id)
        {
            var dto =await _userService.GetAsync(id);
            return Json(dto);
        }
    }
}